package com.aaa.marketresearch.dataresource.dao;

import com.aaa.marketresearch.dataresource.domain.BillContent;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * @author Nail
 */
@Repository
public class BillContentDAOImpl implements BillContentDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<BillContent> getAll() {
        List<BillContent> result = em.createQuery("FROM BillContent bc").getResultList();
        return result;
    }

    @Override
    public BillContent getById(Integer id) {
        final String query = "FROM BillContent bc WHERE bc.id=:id";
        Object obj = em.createQuery(query).setParameter("id", id).getSingleResult();
        if (obj != null) {
            return (BillContent)obj;
        } else {
            return null;
        }
    }

    @Override
    public void save(BillContent billContent) {
        em.merge(billContent);
    }

    @Override
    public boolean delete(Integer id) {
        final String query = "DELETE BillContent bc WHERE bc.id = :id";
        int count = em.createQuery(query).setParameter("id", id).executeUpdate();
        return (count > 0);
    }
}
